import Vue from 'vue'
import App from './App.vue'
import router from './core/router.js'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import { Message } from 'element-ui';
import setting from './core/setting.js'
import screenfull from './core/screenfull.js'
import Loading from './components/commons/loading.vue'
import 'default-passive-events'
import './assets/css/icon.css'
import './core/app.css'
// import './assets/theme/mimicry-theme.less'
// import './assets/theme/arco-pro-theme.less'
import './assets/theme/default-theme.less'

Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.prototype.$axios = axios;
Vue.prototype.$url = setting.baseURL;
Vue.prototype.$setting=setting;
Vue.prototype.$screenfull=screenfull

Vue.component("Loading",Loading);//注册全局组件

let token='';
let adminID='';
axios.defaults.headers.common['token'] = token;
axios.defaults.headers.common['adminID'] = adminID;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
      let user = window.sessionStorage.getItem('token');
      let admin = window.sessionStorage.getItem('admin');

      if (user!=''&&user!=null) {
            admin = JSON.parse(admin);
            adminID = admin.id;
            token = user;
      }
      config.headers.common['token'] = token;
      config.headers.common['adminID'] = adminID;
      
      return config;
}, function (error) {
      return Promise.reject(error);
});
// 添加一个响应拦截器
axios.interceptors.response.use(
      function (response) {
            if (response.data.status.code) {
                  if (parseInt(response.data.status.code) == 401) {
                        //未登录
                        Message({
                              type:'info',
                              message:response.data.status.message
                        })
                        router.push('/login');
                        vue.loading=false;
                  }
            }
            return response;
      }, 
      function (error) {
            console.log(error);
            // router.push('/login');
            Message({
                  type:'info',
                  message:'服务器连接异常'
            })
            vue.loading=false;
      }
)

router.beforeEach((to, from, next) => {
      /* 路由发生变化修改页面title */
      if (to.meta.title) {
            document.title = to.meta.title;
      }else{
            document.title=setting.systemTitle;
      }
      next()
})

let vue=new Vue({
      router,
      render: h => h(App),
      data(){
            return{
                  loading:false // 全局属性，通过$root
            }
      }
}).$mount('#app')